package yxy.game.pm2.active.command.monthfund;

import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actmonthfund;
import yxy.cherry.data.bean.actmonthfunddate;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_超值月基金;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.MonthFundInfo;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActMonthFundInfoRequest;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@MessageType(proto = Command.ActMonthFundInfo, classtype = ActiveMessage.ActMonthFundInfoRequest.class)
public class CMD_Active_超值月基金_Info extends Command<ActMonthFundInfoRequest> {

	private static final Logger logger = Logger.create(CMD_Active_超值月基金_Info.class);
	
	@Override
	public void execute(MessageSource messageSource, ActMonthFundInfoRequest request) throws Exception {
		String playercode = messageSource.getTag();
		
		ActiveMessage.ActMonthFundInfoResponse.Builder response = ActiveMessage.ActMonthFundInfoResponse.newBuilder();

		ActiveModule_超值月基金 am = ActiveModule_超值月基金.get(playercode,false);

		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		ArrayList<Integer> types = new ArrayList<Integer>();
		types.add(ActiveModule_超值月基金.mftype1);
		types.add(ActiveModule_超值月基金.mftype2);
		
		Map<String, actmonthfund> amfmap = ActDataCenter.getAllData(actmonthfund.class, playercode);
		if(amfmap == null || amfmap.size() == 0) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		HashMap<Integer, actmonthfunddate> typemap = new HashMap<Integer, actmonthfunddate>();
		Map<String, actmonthfunddate> amfdmap = ActDataCenter.getAllData(actmonthfunddate.class, playercode);
		for(actmonthfunddate amfd : amfdmap.values()) {
			typemap.put(amfd.getType(), amfd);
		}
		
		for (int type : types) {
			ActiveMessage.MonthFoundInfo.Builder build = ActiveMessage.MonthFoundInfo.newBuilder();
			build.setType(type);
			actmonthfunddate amfd = typemap.get(type);
			if (amfd == null) {
				continue;
			}
			build.setGoodsid(amfd.getChargeid());
			Collection<Currency> curs = new Currency().create(amfd.getRewardtype(), amfd.getRewardid(),	amfd.getRewardnum());
			Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
			build.addAllCur(items); //礼包立即发放奖励

			MonthFundInfo mf = am.getMonthFundByType(type);
			if (mf == null) {
				response.addMfs(build.build());
				continue;
			}

			long buytime = mf.getBuyTime() == null ? 0 : mf.getBuyTime().getMillis();
			build.setBuytime(buytime);
			build.setLogindays(mf.getLogindays());
			build.addAllGetids(mf.getGetids());  //已领取的奖励

			// 每天的奖励
			for (actmonthfund amf : amfmap.values()) {
				if (amf.getType() != type) {
					continue;
				}

				if (mf.getGetids().contains(amf.getId())) {
					continue;
				}

				if (amf.getDay() > mf.getLogindays()) {
					continue;
				}
				build.addCangetids(amf.getId()); //每天可领取奖励
			}

			response.addMfs(build.build());
		}
		
		send(messageSource, response, Result.Success);
	}
	
	private void send(MessageSource messageSource, ActiveMessage.ActMonthFundInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}

}
